cd "C:\Users\bhighton\Desktop\Dropbox\_My Documents_\Collaborations\asymmetry\analysis\senate\lsq replication materials"
set more off

clear
use "cces2010.dta"


* converting variable names to lowercase *
rename _all,lower

gen weight=v101
decode v206,gen(state)
replace state=ltrim(state)
replace state=rtrim(state)
merge m:1 state using "StateCodesMaster.dta" 
drop _merge

*** demographics ***
recode v208 2=1 else=0,gen(female)
label variable female
recode v211 2=1 else=0,gen(black)
label variable black
recode v211 3=1 else=0,gen(latino)
label variable latino
recode v211 4/8 =1 else=0,gen(othrace)
label variable othrace
recode v246 15=.,gen(faminc)
label variable faminc
label values faminc V246
recode faminc .=1 else=0,gen(famincna)
label variable famincna
sum faminc ,d
replace faminc=`r(p50)' if faminc==.


*** ideology ***
recode cc334a 8=.,gen(respideo)
label values respideo LABI
label variable respideo

gen demideo=cc334h if cc334h>=1 & cc334h<=7
label values demideo LABI
label variable demideo


gen repideo=cc334i if cc334i>=1 & cc334i<=7
label values repideo LABI
label variable repideo


*** PID ***
recode v212d (1 2 3=0 "Dem") (5 6 7=2 "Rep") (else=1 "Ind/DK"),gen(pid3)
label variable pid3


*** vote choice ***
recode cc410a (1=0 "Dem")(2=1 "Rep")(else=.),gen(vote)
replace vote=. if state=="Alaska"  // murkowski as write-in
label variable vote


*** Incumbency ***
gen incumb=0
replace incumb=1 if v553==1
replace incumb=-1 if v550==1
replace incumb=. if v550==.|v553==.
replace incumb=. if state=="Alaska"  // murkowski as writein


gen year=2010
keep weight-year
compress
save temp1.dta ,replace


*** computing mean positions ***
clear
use temp1.dta
collapse demideo repideo [pw=weight] ,by(state)
drop if demideo==.|repideo==.
gen demideomean=round(demideo,.001)
gen repideomean=round(repideo,.001)
keep state demideomean repideomean
save temp2.dta ,replace


*** merging ***
clear
use temp1.dta
merge m:1 state using temp2.dta
drop _merge

compress
save cces2010_analysis.dta ,replace
erase temp1.dta
erase temp2.dta
